//package com.zmn.plat.admin.controller.base.phenomenon;
//
//import com.zmn.common.constant.StatusConsts;
//import com.zmn.common.dto2.ResponseDTO;
//import com.zmn.common.utils.date.DateUtil;
//import com.zmn.common.utils.number.NumberUtil;
//import com.zmn.common.utils.request.RequestUtil;
//import com.zmn.common.utils.string.StringUtil;
//import com.zmn.consts.GlobalConsts;
//import com.zmn.plat.admin.controller.PlatBaseController;
//import com.zmn.plat.business.interfaces.base.fault.FaultBService;
//import com.zmn.plat.business.interfaces.base.phenomenon.PhenomenonBService;
//import com.zmn.plat.model.entity.base.fault.Fault;
//import com.zmn.plat.model.entity.base.maintainitem.MaintainItem;
//import com.zmn.plat.model.entity.base.phenomenon.*;
//import com.zmn.plat.services.interfaces.base.fault.FaultService;
//import com.zmn.plat.services.interfaces.base.maintainitem.MaintainItemService;
//import com.zmn.plat.services.interfaces.base.phenomenon.PhenomenonFaultService;
//import com.zmn.plat.services.interfaces.base.phenomenon.PhenomenonMaintainService;
//import com.zmn.plat.services.interfaces.base.phenomenon.PhenomenonService;
//import lombok.extern.slf4j.Slf4j;
//import org.apache.shiro.authz.annotation.RequiresPermissions;
//import org.springframework.http.MediaType;
//import org.springframework.web.bind.annotation.*;
//import org.springframework.web.multipart.MultipartFile;
//
//import javax.annotation.Resource;
//import javax.servlet.http.HttpServletRequest;
//import java.util.Date;
//import java.util.List;
//import java.util.Map;
//import java.util.Optional;
//import java.util.stream.Collectors;
//
///**
// * 故障现象基础修改Controller
// *
// * @author chengguang
// * @since 2021/11/18 17:54
// */
//@Slf4j
//@RestController
//@RequestMapping("/base/phenomenon")
//public class PhenomenonModifyController extends PlatBaseController {
//
//    @Resource
//    private PhenomenonService phenomenonService;
//
//    @Resource
//    private PhenomenonBService phenomenonBService;
//
//    @Resource
//    private FaultService faultService;
//
//    @Resource
//    private MaintainItemService maintainItemService;
//
//    @Resource
//    private PhenomenonFaultService phenomenonFaultService;
//
//    @Resource
//    private PhenomenonMaintainService phenomenonMaintainService;
//
//    @Resource
//    private FaultBService faultBService;
//
//    /**
//     * 描述: 保存
//     *
//     * @author chengguang
//     * @since 2021/11/18 17:54
//     */
//    @RequestMapping("/addEdit")
//    public ResponseDTO addEdit(HttpServletRequest request, @RequestBody Phenomenon phenomenon) {
//        Date now = DateUtil.getNow();
//        Integer phenId = phenomenon.getPhenId();
//        try {
//            phenomenon.setUpdater(getMcStaffRealName());
//            phenomenon.setUpdateTime(now);
//            if (NumberUtil.isNullOrZero(phenId)) {
//                phenomenon.setCreater(getMcStaffRealName());
//                phenomenon.setCreateTime(now);
//                if (NumberUtil.isNullOrZero(phenomenon.getSort())) {
//                    Integer maxSort = Optional.ofNullable(phenomenonService.getMaxSort()).orElse(0);
//                    phenomenon.setSort(++maxSort);
//                }
//                phenomenon.setStatus(GlobalConsts.NO);
//                phenomenonService.insert(phenomenon);
//            } else {
//                phenomenonService.updateByKey(phenomenon);
//            }
//            return ResponseDTO.success(StatusConsts.STATUS_SUCCESS, "操作成功！");
//        } catch (Exception e) {
//            log.error("新增/修改失败,Exception{},msg{}", e, e.getMessage());
//            return ResponseDTO.fail("操作失败！");
//        }
//    }
//
//    /**
//     * 描述: 关联故障
//     *
//     * @param @param manageList 管理列表
//     * @return @return {@link ResponseDTO }
//     * @author chengguang
//     * @since 2021/11/19 14:20
//     */
//    @RequestMapping(value = "/addRelateFault", method = RequestMethod.POST)
//    @RequiresPermissions("/base/phenomenon/addRelateFault.action")
//    public ResponseDTO addRelateFault(@RequestParam(value = "faultIds") String faultIds,
//                                      @RequestParam(value = "phenId") String phenId) {
//        Date now = DateUtil.getNow();
//
//        try {
//            if (StringUtil.isBlank(faultIds)) {
//                return ResponseDTO.fail("请选择要关联的数据");
//            }
//            List<Integer> arrayFaultIds = StringUtil.convertStrsToList(faultIds);
//            List<Fault> manages = faultService.listByIds(arrayFaultIds);
//            Map<Integer, String> managesMap = Optional.of(manages.stream()
//                    .collect(Collectors.toMap(Fault::getFaultId, Fault::getFaultName, (a, b) -> b)))
//                    .orElse(null);
//            List<PhenomenonFault> collect = arrayFaultIds.stream().map(item -> {
//                PhenomenonFault phenManage = new PhenomenonFault();
//                phenManage.setCreateTime(now);
//                phenManage.setUpdateTime(now);
//                phenManage.setCreater(getMcStaffRealName());
//                phenManage.setUpdater(getMcStaffRealName());
//                phenManage.setFaultId(item);
//                phenManage.setPhenId(Integer.parseInt(phenId));
//                phenManage.setFaultName(managesMap.get(item));
//                return phenManage;
//            }).collect(Collectors.toList());
//            phenomenonBService.addRelateFault(collect);
//            return ResponseDTO.success(StatusConsts.STATUS_SUCCESS, "操作成功！");
//        } catch (Exception e) {
//            log.error("新增失败,Exception{},msg{}", e, e.getMessage());
//            return ResponseDTO.fail("操作失败！");
//        }
//    }
//
//    /**
//     * 描述: 关联检修项目
//     *
//     * @param @param deteList
//     * @return @return {@link ResponseDTO }
//     * @author chengguang
//     * @since 2021/11/19 14:20
//     */
//    @PostMapping("/addPhenomenonMaintain")
//    @RequiresPermissions("/base/phenomenon/addPhenomenonMaintain.action")
//    public ResponseDTO addPhenomenonMaintain(@RequestParam(value = "maintainIds") String maintainIds,
//                                             @RequestParam(value = "phenId") String phenId) {
//        Date now = DateUtil.getNow();
//        try {
//            if (StringUtil.isBlank(maintainIds)) {
//                return ResponseDTO.fail("请选择要关联的数据");
//            }
//            List<Integer> arrayMaintainIds = StringUtil.convertStrsToList(maintainIds);
//            List<MaintainItem> maintainItems = maintainItemService.listByIds(arrayMaintainIds);
//            Map<Integer, String> maintainMap = Optional.of(maintainItems.stream()
//                    .collect(Collectors.toMap(MaintainItem::getMaintainId, MaintainItem::getMaintainName, (a, b) -> b)))
//                    .orElse(null);
//            List<PhenomenonMaintain> collect = arrayMaintainIds.stream().map(item -> {
//                PhenomenonMaintain maintain = new PhenomenonMaintain();
//                maintain.setCreateTime(now);
//                maintain.setUpdateTime(now);
//                maintain.setCreater(getMcStaffRealName());
//                maintain.setUpdater(getMcStaffRealName());
//                maintain.setMaintainId(item);
//                maintain.setPhenId(Integer.parseInt(phenId));
//                maintain.setMaintainName(maintainMap.get(item));
//                return maintain;
//            }).collect(Collectors.toList());
//            phenomenonBService.addPhenomenonMaintain(collect);
//            return ResponseDTO.success(StatusConsts.STATUS_SUCCESS, "操作成功！");
//        } catch (Exception e) {
//            log.error("新增失败,Exception{},msg{}", e, e.getMessage());
//            return ResponseDTO.fail("操作失败！");
//        }
//    }
//
//    /**
//     * 描述: 修改状态
//     *
//     * @param ids    id集合
//     * @param status 状态
//     * @author chengguang
//     * @since 2021/11/18 17:54
//     */
//    @RequestMapping(value = "/updateStatus")
//    @RequiresPermissions("/base/phenomenon/updateStatus.action")
//    public ResponseDTO<Boolean> updateStatus(@RequestParam(value = "ids") String ids,
//                                             @RequestParam(value = "status") Integer status) {
//
//        if (StringUtil.isBlank(ids)) {
//            return ResponseDTO.fail("请选择要更改状态的数据！");
//        }
//        List<Integer> phenIds = StringUtil.convertStrsToList(ids);
//        try {
//            Integer faultCount = Optional.ofNullable(phenomenonFaultService.countByQuery(PhenomenonFaultQuery.builder().phenIds(phenIds).build())).orElse(GlobalConsts.NONE);
//            Integer maintainCount = Optional.ofNullable(phenomenonMaintainService.countByQuery(PhenomenonMaintainQuery.builder().phenIds(phenIds).build())).orElse(GlobalConsts.NONE);
//            if (faultCount == 0 || maintainCount == 0) {
//                return ResponseDTO.fail("请先关联故障和检修项目，再开启故障现象");
//            }
//            phenomenonService.updateStatusBatch(phenIds, status, getMcStaffRealName());
//            return ResponseDTO.success(true, "操作成功！");
//        } catch (Exception e) {
//            log.error("{},{}", e.getMessage(), e);
//            return ResponseDTO.fail("修改状态失败");
//        }
//    }
//
//    /**
//     * 功能描述: 修改排序
//     *
//     * @param phenId 主键
//     * @param sort   排序
//     * @author chengguang
//     * @since 2021/11/18 17:54
//     */
//    @RequestMapping(value = "/updateSort")
//    public ResponseDTO<Boolean> updateStatus(@RequestParam(value = "id") Integer phenId,
//                                             @RequestParam(value = "sort") Integer sort) {
//
//        if (NumberUtil.isNullOrZero(phenId)) {
//            return ResponseDTO.fail("请选择要更改排序的数据");
//        }
//        if (NumberUtil.isNullOrZero(sort)) {
//            return ResponseDTO.fail("请设置排序值");
//        }
//        try {
//            Phenomenon phenomenon = Phenomenon.builder()
//                    .phenId(phenId)
//                    .sort(sort)
//                    .updater(getMcStaffRealName())
//                    .updateTime(DateUtil.getNow())
//                    .build();
//            phenomenonService.updateByKey(phenomenon);
//            return ResponseDTO.success(true, "操作成功！");
//        } catch (Exception e) {
//            return ResponseDTO.fail("修改排序失败");
//        }
//    }
//    /**
//     * 上传文件
//     *
//     * @author liuyong
//     * @since 2021-04-26 16:06:44
//     */
//
//    @RequestMapping(value = "/uploadExcel", method = RequestMethod.POST, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
//    public ResponseDTO uploadExcel(HttpServletRequest request, @RequestPart(value = "file") MultipartFile file) {
//        String operator = getMcStaffRealName();
//        Integer operatorId = getMcStaffId();
//        String ip = RequestUtil.getRequestIp(request);
//        ResponseDTO responseDTO = phenomenonBService.importExcel(file, operatorId, operator, ip);
//        faultBService.importTemplateFile(file);
//
//        return responseDTO;
//    }
//
//
//}
